<?php

    if (!defined('BASEPATH'))
        exit('No direct script access allowed');

    class Excursions extends CI_Controller {

        public function __construct() {
            parent::__construct();

            // Load the rest client spark
            $this->load->spark('restclient/2.1.0');

            // Run some setup
            $this->rest->initialize(array(
                'server' => REST_URL_HOTELS,
                'http_user' => 'cipa',
                'http_pass' => 'booking',
                'http_auth' => 'basic' // or 'digest' 
            ));

            $this->activeLang = $this->lang->mci_current();

            require_once EXTERNAL_URL . 'urlify.php';

            // Do caching
            $this->load->driver('cache', array('adapter' => 'file')); // za uklucuvanje na kes mogucnosti
        }

        public function index() {
            $this->search();
        }

        public function search() {

            $rest_data = array(
                'lang' => $this->activeLang
            );

            $file = cache_filename($rest_data, 'excursions_list');

            if (!$data['results'] = $this->cache->get($file)) {

                $data['results'] = $this->rest->post('searchExcursions', $rest_data);

                if (DO_CACHE)
                    $this->cache->save($file, $data['results'], 300);
            }

            $data['tags'] = $data['results']->tags;
            $data['results'] = $data['results']->excursions;


            $data['seo_title'] = lang('title-excursions');
            $data['seo_description'] = lang('title-excursions');
            $data['seo_keywords'] = lang('description-excursions');
            $data['h1_title'] = lang('h1-excursions');

//            $this->firephp->log($data);

            $this->template->load('excursions/search', $data);
        }

        public function detailsExc($ID) {

            $ID = explode('-', $ID);
            $ID = $ID[count($ID) - 1];

            $rest_method = 'detailsExcursions';
            $rest_data = array(
                'excursion_id' => $ID,
                'lang' => $this->activeLang
            );

            $data['details'] = $this->rest->post($rest_method, $rest_data);

//            $this->firephp->log($data['details']);

            $data['similar'] = $data['details']->similar;
            unset($data['details']->similar);

            $data['seo_title'] = "Excursions in Montenegro | Cipatravel";
            $data['seo_description'] = "Best choise of excursions in montenegro";
            $data['seo_keywords'] = "Online booking excursions Montenegro";
            $data['h1_title'] = $data['details']->name;

//            $this->firephp->log($data['details']);

            $this->load->config('countries');
            $this->template->load('excursions/details', $data);
        }

        public function availability() {

            $rest_method = 'availabilityExcursions';
            $rest_data = array(
                'excursion_id' => $_GET['exc_id'],
                'adl' => $_GET['nr_adults'],
                'chd' => $_GET['nr_childrens'],
                'date' => $_GET['check_in'],
                'lang' => $this->activeLang
            );

            $data = $this->rest->post($rest_method, $rest_data);

            $data->form->message = "";

            $data->form->service_total = $data->form->service_price;

//            $this->firephp->log($data);

            if ($data->form->isAvailiable == 0):

                $data->form->total_price = 0;
                switch ($data->form->isAvailiableKey):


                    case 'minPS':
                        $data->form->message = lang('min_ps') . $data->form->isAvailiableData . '.';

                        break;

                    case 'places':
                        $data->form->message = lang('places_left') . $data->form->isAvailiableData . '.';

                        break;

                    case 'sellingSeason':
                        $data->form->message = lang('sellingSeason') . $data->form->isAvailiableData . '.';

                endswitch;

            endif;

            echo json_encode($data->form);
        }

        public function reservation() {

            unset($_GET['ages']);

            $rest_method = 'checkoutViewExcursions';
            $rest_data = array(
                'excursion_id' => $_GET['exc_id'],
                'adl' => $_GET['nr_adults'],
                'chd' => $_GET['nr_childrens'],
                'date' => $_GET['check_in'],
                'lang' => $this->activeLang
            );

            $data['reservation'] = $this->rest->post($rest_method, $rest_data);
//            $this->firephp->log($data['reservation']);

            $data['seo_title'] = lang('title-reservation_details');
            $data['seo_description'] = "";
            $data['seo_keywords'] = "";

            $cookie = array(
                'name' => 'cipa_cookie',
                'value' => '1',
                'expire' => '86500'
            );

            $this->input->set_cookie($cookie); // Set coockie for redirecting on confirmation page reload

            $this->load->config('countries');
            $this->template->set_view('template_checkout');
            $this->template->load('site/checkoutExcursions', $data);
        }

        /*
         * Customer Form Show Object and room with prices
         * 
         * @params:
         *      check_in
         *      check_out
         *      nr_adults
         *      ages
         *      hotel_id
         *      room_id
         * 
         */

        public function changedatesExc() {

            unset($_GET['ages']);

            $rest_method = 'checkoutViewExcursions';
            $rest_data = array(
                'excursion_id' => $_GET['exc_id'],
                'adl' => $_GET['nr_adults'],
                'chd' => $_GET['nr_childrens'],
                'date' => $_GET['check_in'],
                'lang' => $this->activeLang
            );

            $data['reservation'] = $this->rest->post($rest_method, $rest_data);
            $data['details'] = $data['reservation'];

            $data['details']->form->firstAvailableDay = $_GET['check_in'];

            $data['seo_title'] = lang('title-change_dates');
            $data['seo_description'] = "";
            $data['seo_keywords'] = "";

//            $this->firephp->log($data['details']);

            $this->template->set_view('template_checkout');
            $this->template->load('site/checkout_datesExcursions', $data);
        }

    }
    